package cn.iocoder.yudao.module.infrastructure.notice.controller.app;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infrastructure.notice.controller.admin.vo.NoticeRespVO;
import cn.iocoder.yudao.module.infrastructure.notice.dal.dataobject.NoticeDO;
import cn.iocoder.yudao.module.infrastructure.notice.dal.mysql.NoticeMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 通知公告") // FIXME 新增
@RestController
@RequestMapping("/system/notice")
@Validated
public class AppNoticeController {

    @Resource
    private NoticeMapper noticeMapper;

    @GetMapping("/list")
    @Operation(summary = "获取通知公告列表")
    @Parameter(name = "limit", description = "数量", required = true, example = "10")
    @PermitAll
    public CommonResult<List<NoticeRespVO>> getNoticeList(@RequestParam("limit") Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 10;
        }
        CoreLambdaQueryWrapper<NoticeDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(NoticeDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
        wrapper.limitN(limit);
        wrapper.orderByDesc(NoticeDO::getId);
        List<NoticeDO> list = noticeMapper.selectList(wrapper);
        return success(BeanUtils.toBean(list, NoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获取通知公告列表")
    @PermitAll
    public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated PageParam reqVO) {
        PageResult<NoticeDO> pageResult = noticeMapper.selectPage(reqVO, new CoreLambdaQueryWrapper<NoticeDO>()
                .eq(NoticeDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .orderByDesc(NoticeDO::getId));
        return success(BeanUtils.toBean(pageResult, NoticeRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<NoticeRespVO> getNotice(@RequestParam("id") Long id) {
        NoticeDO notice = noticeMapper.selectById(id);
        return success(BeanUtils.toBean(notice, NoticeRespVO.class));
    }

}
